package com.gzx.plugin.fin.tech.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class DataGeneratorUtil {
    private static final String[] MOBILE_PREFIX = {"13", "14", "15", "17", "18", "19"};
    private static final String[] AREA_CODES = {"110000", "310000", "440300"}; // 北京/上海/深圳区号示例

    // 生成1000个随机手机号
    public static List<String> generateMobileNumbers() {
        List<String> mobiles = new ArrayList<>(1000);
        Random random = new Random();

        for (int i = 0; i < 1000; i++) {
            // 1. 随机选择号段
            String prefix = MOBILE_PREFIX[random.nextInt(MOBILE_PREFIX.length)];

            // 2. 生成后9位数字
            StringBuilder sb = new StringBuilder(prefix);
            for (int j = 0; j < 9; j++) {
                sb.append(random.nextInt(10));
            }

            // 3. 验证长度并添加
            String number = sb.toString();
            if (number.length() == 11) {
                mobiles.add(number);
            }
        }
        return mobiles;
    }

    // 生成1000个格式合法的身份证号（不保证校验码绝对正确）
    public static List<String> generateIDCards() {
        List<String> idCards = new ArrayList<>(1000);
        Random random = new Random();

        for (int i = 0; i < 1000; i++) {
            // 1. 生成地区码
            String areaCode = AREA_CODES[random.nextInt(AREA_CODES.length)];

            // 2. 生成出生日期（1900-当前年份）
            LocalDate birthDate = randomBirthDate();
            String birthStr = birthDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

            // 3. 生成顺序码（3位）
            String sequence = String.format("%03d", random.nextInt(1000));

            // 4. 生成校验码（简单实现）
            String checkCode = generateCheckCode(areaCode + birthStr + sequence);

            idCards.add(areaCode + birthStr + sequence + checkCode);
        }
        return idCards;
    }

    // 生成随机出生日期
    private static LocalDate randomBirthDate() {
        long minDay = LocalDate.of(1900, 1, 1).toEpochDay();
        long maxDay = LocalDate.now().toEpochDay();
        long randomDay = ThreadLocalRandom.current().nextLong(minDay, maxDay);
        return LocalDate.ofEpochDay(randomDay);
    }

    // 生成简易校验码（实际应使用ISO 7064标准）
    private static String generateCheckCode(String first17) {
        char[] chars = first17.toCharArray();
        int sum = 0;
        for (char c : chars) {
            sum += Character.getNumericValue(c);
        }
        return sum % 10 == 0 ? "X" : String.valueOf(sum % 10);
    }

    // 测试运行
    public static void main(String[] args) {
        List<String> mobiles = generateMobileNumbers();
        System.out.println("手机号示例: " + mobiles.subList(0, 3));

        List<String> idCards = generateIDCards();
        System.out.println("身份证示例: " + idCards.subList(0, 3));
    }
}
